﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Shopping_List.Forms
{
    public partial class DataEdit : Form
    {
        #region Variables
        MainForm pubvars = new MainForm();
        #endregion
        
        #region Public Functions
        public void SaveDB()
        {
            try
            {
                StreamWriter writer = new StreamWriter(pubvars.DatabaseFileName);
                writer.WriteLine(pubvars.DatabaseCookie);
                for (int i = 0; i < MainTree.Nodes.Count; i++)
                {
                    writer.WriteLine(MainTree.Nodes[i].Text);
                    for (int j = 0; j < MainTree.Nodes[i].Nodes.Count; j++)
                    {
                        writer.WriteLine(MainTree.Nodes[i].Nodes[j].Text);
                    }
                    writer.WriteLine("?END");
                }
                writer.Close();
                MessageBox.Show("Successfully Saved!");
            }
            catch
            {
                MessageBox.Show("An Error Occured!");
            }
        }

        void SaveFaves()
        {
            try
            {
                StreamWriter writer = new StreamWriter(pubvars.FavesFileName);
                writer.WriteLine(pubvars.FavesCookie);
                foreach (String item in FavesList.Items)
                {
                    writer.WriteLine(item);
                }
                writer.Close();
            }
            catch
            {
                MessageBox.Show("An error occured", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Initialization
        public DataEdit()
        {
            InitializeComponent();
        }

        private void DataEdit_Load(object sender, EventArgs e)
        {
            pubvars.Load_Database(MainTree);
            pubvars.LoadList(pubvars.FavesFileName, pubvars.FavesCookie, FavesList);
        }
        #endregion

        #region Events
        private void MainTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (MainTree.SelectedNode != null)
            {
                if (MainTree.SelectedNode.Level == 0)
                {
                    NewItem.Enabled   = true;
                    NewItem.BackColor = pubvars.ButtonPlain;
                    NewItem.Cursor    = Cursors.Hand;
                    AddToFavourites.BackColor = pubvars.ButtonDown;
                    AddToFavourites.Enabled = false;
                }
                else
                {
                    NewItem.Enabled   = false;
                    NewItem.BackColor = pubvars.ButtonDown;
                    NewItem.Cursor = Cursors.Default;
                    AddToFavourites.BackColor = pubvars.ButtonPlain;
                    AddToFavourites.Enabled = true;
                }
                Delete.Enabled = true;
                Delete.BackColor = pubvars.ButtonPlain;
                Delete.Cursor = Cursors.Hand;
                Rename.Enabled   = true;
                Rename.BackColor = pubvars.ButtonPlain;
                Rename.Cursor   = Cursors.Hand;
            }
            else
            {
                Delete.Enabled = false;
                Delete.BackColor = pubvars.ButtonDown;
                Delete.Cursor = Cursors.Default;
                Rename.Enabled = false;
                Rename.BackColor = pubvars.ButtonDown;
                Rename.Cursor = Cursors.Default;
            }
        }

        Text text = new Text();
        private void NewCategory_Click(object sender, EventArgs e)
        {
            text.LoadInput(0);
            text.InsertText.Text = "";
            if (text.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MainTree.Nodes.Add(text.Result);
            }
        }

        private void NewItem_Click(object sender, EventArgs e)
        {
            text.LoadInput(1);
            text.InsertText.Text = "";
            if (text.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MainTree.Nodes[MainTree.SelectedNode.Index].Nodes.Add(text.Result);
                MainTree.Nodes[MainTree.SelectedNode.Index].Expand();
            }
        }

        private void Rename_Click(object sender, EventArgs e)
        {
            text.InsertText.Text = MainTree.SelectedNode.Text;
            text.LoadInput(2);
            if (text.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                MainTree.SelectedNode.Text = text.Result;
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            MainTree.Nodes.Remove(MainTree.SelectedNode);
        }

        private void Faves_Click(object sender, EventArgs e)
        {
            if (Favourites.Visible == false)
            {
                Favourites.Visible = true;
                Faves.BackgroundImage = Properties.Resources.CloseFavourites;
            }
            else
            {
                Favourites.Visible = false;
                Faves.BackgroundImage = Properties.Resources.OpenFavourites;
            }
            pubvars.Save_Options();
        }

        private void SortButton_Click(object sender, EventArgs e)
        {
            MainTree.Sort();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            SaveDB();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            SaveDB();
            Close();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure? All unsaved work will be lost.", "Shopping List", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                Close();
            }
        }

        private void AddToFavourites_Click(object sender, EventArgs e)
        {
            FavesList.Items.Add(MainTree.SelectedNode.Text);
            SaveFaves();
        }

        private void RemoveFromFavourites_Click(object sender, EventArgs e)
        {
            FavesList.Items.Remove(FavesList.SelectedItem);
            SaveFaves();
        }

        private void FavesList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FavesList.SelectedIndex != -1)
            {
                RemoveFromFavourites.BackColor = pubvars.ButtonPlain;
                RemoveFromFavourites.Enabled = true;
            }
            else
            {
                RemoveFromFavourites.BackColor = pubvars.ButtonDown;
                RemoveFromFavourites.Enabled = false;
            }
        }
        #endregion region

        #region Special Stuff
        private void NewCategory_MouseEnter(object sender, EventArgs e)
        {
            NewCategory.BackColor = pubvars.Button_OverButton;
        }

        private void NewCategory_MouseLeave(object sender, EventArgs e)
        {
            NewCategory.BackColor = pubvars.ButtonPlain;
        }

        private void NewCategory_MouseDown(object sender, MouseEventArgs e)
        {
            NewCategory.BackColor = pubvars.ButtonDown;
        }

        private void NewCategory_MouseUp(object sender, MouseEventArgs e)
        {
            NewCategory.BackColor = pubvars.ButtonPlain;
        }

        private void NewItem_MouseEnter(object sender, EventArgs e)
        {
            NewItem.BackColor = pubvars.Button_OverButton;
        }

        private void NewItem_MouseLeave(object sender, EventArgs e)
        {
            NewItem.BackColor = pubvars.ButtonPlain;
        }

        private void NewItem_MouseDown(object sender, MouseEventArgs e)
        {
            NewItem.BackColor = pubvars.ButtonDown;
        }

        private void NewItem_MouseUp(object sender, MouseEventArgs e)
        {
            NewItem.BackColor = pubvars.ButtonPlain;
        }

        private void Rename_MouseEnter(object sender, EventArgs e)
        {
            Rename.BackColor = pubvars.Button_OverButton;
        }

        private void Rename_MouseLeave(object sender, EventArgs e)
        {
            Rename.BackColor = pubvars.ButtonPlain;
        }

        private void Rename_MouseDown(object sender, MouseEventArgs e)
        {
            Rename.BackColor = pubvars.ButtonDown;
        }

        private void Rename_MouseUp(object sender, MouseEventArgs e)
        {
            Rename.BackColor = pubvars.ButtonPlain;
        }

        private void Delete_MouseEnter(object sender, EventArgs e)
        {
            Delete.BackColor = pubvars.Button_OverButton;
        }

        private void Delete_MouseLeave(object sender, EventArgs e)
        {
            Delete.BackColor = pubvars.ButtonPlain;
        }

        private void Delete_MouseDown(object sender, MouseEventArgs e)
        {
            Delete.BackColor = pubvars.ButtonDown;
        }

        private void Delete_MouseUp(object sender, MouseEventArgs e)
        {
            Delete.BackColor = pubvars.ButtonPlain;
        }

        private void Faves_MouseEnter(object sender, EventArgs e)
        {
            Faves.BackColor = pubvars.Button_OverButton;
        }

        private void Faves_MouseLeave(object sender, EventArgs e)
        {
            
            Faves.BackColor = pubvars.ButtonPlain;
        }

        private void Faves_MouseDown(object sender, MouseEventArgs e)
        {
            Faves.BackColor = pubvars.ButtonDown;
        }

        private void Faves_MouseUp(object sender, MouseEventArgs e)
        {
            Faves.BackColor = pubvars.ButtonPlain;
        }

        private void SaveButton_MouseEnter(object sender, EventArgs e)
        {
            SaveButton.BackColor = pubvars.Button_OverButton;
        }

        private void SaveButton_MouseLeave(object sender, EventArgs e)
        {
            SaveButton.BackColor = pubvars.ButtonPlain;
        }

        private void SaveButton_MouseDown(object sender, MouseEventArgs e)
        {
            SaveButton.BackColor = pubvars.ButtonDown;
        }

        private void SaveButton_MouseUp(object sender, MouseEventArgs e)
        {
            SaveButton.BackColor = pubvars.ButtonPlain;
        }

        private void OK_MouseEnter(object sender, EventArgs e)
        {
            OK.BackColor = pubvars.Button_OverButton;
        }

        private void OK_MouseLeave(object sender, EventArgs e)
        {
            OK.BackColor = pubvars.ButtonPlain;
        }

        private void OK_MouseDown(object sender, MouseEventArgs e)
        {
            OK.BackColor = pubvars.ButtonDown;
        }

        private void OK_MouseUp(object sender, MouseEventArgs e)
        {
            OK.BackColor = pubvars.ButtonPlain;
        }

        private void Cancel_MouseEnter(object sender, EventArgs e)
        {
            Cancel.BackColor = pubvars.Button_OverButton;
        }

        private void Cancel_MouseLeave(object sender, EventArgs e)
        {
            Cancel.BackColor = pubvars.ButtonPlain;
        }

        private void Cancel_MouseDown(object sender, MouseEventArgs e)
        {
            Cancel.BackColor = pubvars.ButtonDown;
        }

        private void Cancel_MouseUp(object sender, MouseEventArgs e)
        {
            Cancel.BackColor = pubvars.ButtonPlain;
        }

        private void SortButton_MouseEnter(object sender, EventArgs e)
        {
            SortButton.BackColor = pubvars.Button_OverButton;
        }

        private void SortButton_MouseLeave(object sender, EventArgs e)
        {
            SortButton.BackColor = pubvars.ButtonPlain;
        }

        private void SortButton_MouseDown(object sender, MouseEventArgs e)
        {
            SortButton.BackColor = pubvars.ButtonDown;
        }

        private void SortButton_MouseUp(object sender, MouseEventArgs e)
        {
            SortButton.BackColor = pubvars.ButtonPlain;
        }
        #endregion
    }
}
